/**
 * 摇杆类
 * author: suyuyi
 * date :   2017-4-4
 */
cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // use this for initialization
    onLoad: function () {
       
        this._speed = 3;
        this.touchValid = true;
        this.touchAreaRedius = 100;
        this.touchStopRedius = 30;
        this.circleCenter = null;
        this.circleDistance = 0;
       // this.avatar = cc.find("hero/spr_avatar",this.parent);
       // cc.log(this.avatar);
       this.hero = cc.find("hero",this.node.parent);
       this.avatar = cc.find("hero/spr_avatar", this.node.parent);
       this.arrow = cc.find("hero/spr_avatar/spr_arrow",this.node.parent);
       this.arrow.opacity = 0;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchBegin, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);

    },
    onTouchBegin: function(event){
    
        if(this.touchValid == false) return false;
            this.arrow.opacity = 255;
        var touchPos = event.getLocation();
        
        this.circleCenter = touchPos;

    },
     onTouchMove: function(event) {
         

        var localPos = event.getLocation();
        
        this.circleDistance = this.distance(this.circleCenter, localPos);

        if(this.circleDistance >= this.touchStopRedius){
             this.updateStick(localPos);
            var arrowAndTargetDistance = this.circleDistance;
            if (arrowAndTargetDistance < 80) {
                arrowAndTargetDistance = 80;
            }
            else if (arrowAndTargetDistance > 300) {
                arrowAndTargetDistance = 300;
            }
            this.arrow.x = arrowAndTargetDistance;
        }
        else {
            this.touchValid = false;
            
            this.stopStick();
        }

        event.stopPropagation();
    },

    onTouchEnd: function(event) {
        this.stopStick();
        this.touchValid = true;
        this.arrow.opacity = 0;
    //  var distace = this.circleDistance + 400;

    //     var action = cc.moveTo(0.5, 
    //         distace * Math.sin(Math.PI / 180 *(this.arrow.rotation + 90)), 
    //         distace * Math.cos(Math.PI / 180 *(this.arrow.rotation + 90))
    //     );
    //     this.arrow.runAction(action);
        event.stopPropagation();
    },

    onTouchCancel: function(event) {
        this.stopStick();
        this.touchValid = true;
        this.arrow.opacity = 0;
        event.stopPropagation();
    },

    updateStick: function(pos) {

        //结果为正表示从 X 轴逆时针旋转的角度，结果为负表示从 X 轴顺时针旋转的角度。
        //
        var angle = Math.atan2(pos.y - this.circleCenter.y, pos.x - this.circleCenter.x);//计算弧度
       
        var angleHero = angle *  (180 / Math.PI); //转换为度
       
        this.avatar.rotation = -angleHero;
        
        this.hero.x += Math.cos( angle ) * this._speed;  
        this.hero.y += Math.sin( angle ) * this._speed;
    },

    stopStick: function(){
        var event = new cc.Event.EventCustom('joystick_stop', true);
        this.node.dispatchEvent(event);
    },
    distance: function(pos1, pos2){
        return Math.sqrt(Math.pow(pos1.x - pos2.x, 2) +  Math.pow(pos1.y - pos2.y, 2));
    }
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
